package com.chongbangbang.controller.pet;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.chongbangbang.pojo.PetVaccinateInfo;
import com.chongbangbang.pojo.dto.PetVaccinationInfoDTO;
import com.chongbangbang.service.PetVaccinateInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 宠物基本信息之疫苗接种信息 前端控制器
 * </p>
 *
 * @author TP
 * @since 2022-03-16 15:20:37
 */
@RestController
@RequestMapping("/chong/pet/vaccinate")
@Api(tags = {"pet vaccinate info", "vaccinate"}, value = "宠物基本信息之疫苗接种信息")
public class PetVaccinateInfoController {

    @Autowired
    private PetVaccinateInfoService petVaccinateInfoService;

    @ApiOperation("查询所有")
    @RequestMapping(value = "", method = RequestMethod.GET)
    public List<PetVaccinateInfo> get() {
        return petVaccinateInfoService.list();
    }


    @ApiOperation("根据ID查询")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public PetVaccinateInfo getById(@PathVariable Integer id) {
        return petVaccinateInfoService.getById(id);
    }

    @ApiOperation("条件查询")
    @RequestMapping(value = "/query", method = RequestMethod.GET)
    public List<PetVaccinateInfo> getByCondition(PetVaccinationInfoDTO petVaccinationInfoDTO) {
        System.out.println("\n\n"+petVaccinationInfoDTO+"\n");
        QueryWrapper<PetVaccinateInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", petVaccinationInfoDTO.getUserId());
        queryWrapper.ge("vaccinate_time", petVaccinationInfoDTO.getBeginTime());
        queryWrapper.le("vaccinate_time", petVaccinationInfoDTO.getEndTime());
        if (petVaccinationInfoDTO.getPetId() != null) {
            queryWrapper.eq("pet_id", petVaccinationInfoDTO.getPetId());
        }
        if (!StringUtils.isEmpty(petVaccinationInfoDTO.getVaccinateId() +"")) {
            queryWrapper.eq("vaccinate_id", petVaccinationInfoDTO.getVaccinateId());
        }
        return petVaccinateInfoService.list(queryWrapper);
    }


    @ApiOperation("根据宠物ID查询")
    @RequestMapping(value = "/pet/{petId}", method = RequestMethod.GET)
    public List<PetVaccinateInfo> getByPetId(@PathVariable Integer petId) {
        QueryWrapper<PetVaccinateInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pet_id", petId);
        return petVaccinateInfoService.list(queryWrapper);
    }


    @ApiOperation("单个插入")
    @RequestMapping(value = "", method = RequestMethod.POST)
    public boolean post(PetVaccinateInfo petVaccinateInfo) {
        return petVaccinateInfoService.save(petVaccinateInfo);
    }


    @ApiOperation("批量插入")
    @RequestMapping(value = "/_mcreate", method = RequestMethod.POST)
    public boolean postBatches(@ApiParam(value = "传ArrayList<PetVaccinateInfo>") ArrayList<PetVaccinateInfo> list) {
        return petVaccinateInfoService.saveBatch(list);
    }


    @ApiOperation("根据ID更新")
    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    public boolean putById(@ApiParam(name = "id", value = "id要单独传") @PathVariable("id") Integer id, PetVaccinateInfo petVaccinateInfo) {
        return petVaccinateInfoService.updateById(petVaccinateInfo);
    }

    @ApiOperation("批量更新")
    @RequestMapping(value = "/_mupdate", method = RequestMethod.PUT)
    public boolean putBatches(@ApiParam(value = "传ArrayList<PetVaccinateInfo>") ArrayList<PetVaccinateInfo> list) {
        return petVaccinateInfoService.updateBatchById(list);
    }


    @ApiOperation("根据ID删除")
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public boolean deleteById(@PathVariable Integer id) {
        return petVaccinateInfoService.removeById(id);
    }

    @ApiOperation("根据pet_id删除")
    @RequestMapping(value = "/pet/{petId}", method = RequestMethod.DELETE)
    public boolean deleteByPetId(@PathVariable Integer petId) {
        QueryWrapper<PetVaccinateInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pet_id", petId);
        return petVaccinateInfoService.remove(queryWrapper);
    }


    @ApiOperation("根据ID批量删除")
    @RequestMapping(value = "/_mdelete", method = RequestMethod.DELETE)
    public boolean deleteById(@ApiParam(value = "传ArrayList<Integer>") ArrayList<Integer> list) {
        return petVaccinateInfoService.removeById(list);
    }

}
